home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / PopupMenu.java < prev    next >
Text File  |  1998-09-22  |  3KB  |  111 lines

  1. /*
  2.  * @(#)PopupMenu.java    1.13 98/08/21
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14.  
  15. package java.awt;
  16.  
  17. import java.awt.peer.PopupMenuPeer;
  18.  
  19.  
  20. /**
  21.  * A class that implements a menu which can be dynamically popped up
  22.  * at a specified position within a component.
  23.  *
  24.  * @version    1.13 08/21/98
  25.  * @author     Amy Fowler
  26.  */
  27. public class PopupMenu extends Menu {
  28.  
  29.     private static final String base = "popup";
  30.     static int nameCounter = 0;
  31.  
  32.     /*
  33.      * JDK 1.1 serialVersionUID 
  34.      */
  35.     private static final long serialVersionUID = -4620452533522760060L;
  36.  
  37.     /**
  38.      * Creates a new popup menu.
  39.      */
  40.     public PopupMenu() {
  41.     this("");
  42.     }
  43.  
  44.     /**
  45.      * Creates a new popup menu with the specified name.
  46.      * @param title the title string for the popup menu
  47.      */
  48.     public PopupMenu(String label) {
  49.     super(label);
  50.     }
  51.  
  52.     /**
  53.      * Construct a name for this MenuComponent.  Called by getName() when
  54.      * the name is null.
  55.      */
  56.     String constructComponentName() {
  57.     return base + nameCounter++;
  58.     }
  59.  
  60.     /**
  61.      * Creates the popup menu's peer.  The peer allows us to change the 
  62.      * appearance of the popup menu without changing any of the popup menu's 
  63.      * functionality.
  64.      */
  65.     public void addNotify() {
  66.       synchronized (getTreeLock()) {
  67.     if (peer == null) {
  68.         peer = Toolkit.getDefaultToolkit().createPopupMenu(this);
  69.     }
  70.     int nitems = getItemCount();
  71.     for (int i = 0 ; i < nitems ; i++) {
  72.         MenuItem mi = getItem(i);
  73.         mi.parent = this;
  74.         mi.addNotify();
  75.     }
  76.       }
  77.     }
  78.  
  79.    /**
  80.      * Shows the popup menu at the x, y position relative to an origin component.
  81.      * The origin component must be contained within the component hierarchy 
  82.      * of the popup menu's parent.  Both the origin and the parent must be 
  83.      * showing on the screen for this method to be valid.
  84.      * @param origin the component which defines the coordinate space
  85.      * @param x the x coordinate position to popup the menu
  86.      * @param y the y coordinate position to popup the menu
  87.      */
  88.     public void show(Component origin, int x, int y) {
  89.     Component p = (Component)parent;
  90.     if (p == null) {
  91.         throw new NullPointerException("parent is null");
  92.     }
  93.     if (p != origin &&
  94.         p instanceof Container && !((Container)p).isAncestorOf(origin)) {
  95.         throw new IllegalArgumentException("origin not in parent's hierarchy");
  96.     }
  97.     if (p.getPeer() == null || !p.isShowing()) {
  98.         throw new RuntimeException("parent not showing on screen");
  99.     }
  100.     if (peer == null) {
  101.         addNotify();
  102.     }
  103.         synchronized (getTreeLock()) {
  104.             if (peer != null) {
  105.                 ((PopupMenuPeer)peer).show(
  106.                     new Event(origin, 0, Event.MOUSE_DOWN, x, y, 0, 0));
  107.             }
  108.         }
  109.     }
  110. }
  111.